package com.achievement.task;
import com.achievement.domain.entity.ExpertReviewEntity;
import com.achievement.domain.entity.HonorApplyInfoEntity;
import com.achievement.service.IHonorApplyInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.common.msg.service.SendMessageService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.List;

/**
 * 处理成果申报计划定时任务
 */
@Component
public class HonorApplyInfoScheduler {
    @Autowired
    private IHonorApplyInfoService honorApplyInfoService;
    public HonorApplyInfoScheduler(IHonorApplyInfoService honorApplyInfoService) {
        this.honorApplyInfoService = honorApplyInfoService;
    }


    /**
     * 申报计划到时间到了自动停止
     */
    // 每2小时执行一次
    @Scheduled(fixedDelay = 2 * 60 * 60 * 1000)
    @Transactional
    public void honorApplyInfoEnd() {
        // 查询需要更新的记录
        LambdaQueryWrapper<HonorApplyInfoEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(HonorApplyInfoEntity::getApplyState, "1");
        queryWrapper.eq(HonorApplyInfoEntity::getIsAutoEnd, "1");
        //时间大于当前时间
        // 获取今天的结束时间 2025-05-09 23:59:59
        LocalDateTime endOfToday = LocalDateTime.of(
                LocalDate.now(),
                LocalTime.MAX
        );
        queryWrapper.gt(HonorApplyInfoEntity::getEndDate, endOfToday);
        List<HonorApplyInfoEntity> entities=honorApplyInfoService.getListByConditions(queryWrapper);
        if(ObjectUtils.isNotEmpty(entities)&&entities.size()>0){
            for (HonorApplyInfoEntity item:entities
            ) {
                UpdateWrapper<HonorApplyInfoEntity> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("id", item.getId());
                updateWrapper.set("APPLY_STATE", "2");
                honorApplyInfoService.update(updateWrapper);
            }
        }
    }
}
